package sz.szsmk.citizencard.mvp.presenter;

import android.widget.Toast;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.GetCodeRequest;
import sz.szsmk.citizencard.model.network.bean.request.SpecialAuthReq;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.mvp.contract.ISpecialAuthContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.AndroidUtils;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2019
 * author: zdh
 * created on: 2019/1/8 22:03
 * description:
 */
public class SpecialAuthPresenter extends IBasePresenter
        implements ISpecialAuthContact.ISpecialAuthPresenter<ISpecialAuthContact.ISpecialAuthView> {

    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public SpecialAuthPresenter(GsdxNetApi gsdxNetApi) {
        this.mNetworkDateSource = NetworkDateSources.getInstance();
        this.gsdxNetApi = gsdxNetApi;
    }

    @Override
    public void getSms(String phone) {
        GetCodeRequest getCodeRequest = new GetCodeRequest();
        String androidId = AndroidUtils.getAndroidId(DaggerApplication.getIns());

        getCodeRequest.setDevice_id(androidId);
        getCodeRequest.setChannel_id("000001");
        getCodeRequest.setType_id("9");
        getCodeRequest.setMobile(phone);
        String json = new Gson().toJson(getCodeRequest);
        L.e(Constant.TAG, "getCode:" + json);
        mNetworkDateSource.getSms(getCodeRequest, new SubscribeResult<BaseBean>() {
            @Override
            protected void onFailure(BaseBean response) {
                Toast.makeText(DaggerApplication.getIns(), response.getMessage(), Toast.LENGTH_SHORT).show();
//                ((IGetCodeContact.IGetCodeView) mView).getCodeFail(response.getMessage());
            }

            @Override
            protected void onOk(BaseBean response) {

//                PubUtils.popTipOrWarn(DaggerApplication.getIns(), response.getMessage());
//                ((IGetCodeContact.IGetCodeView) mView).updataSuccess();
            }

            @Override
            protected void onNext(BaseBean response) {
//                PubUtils.popTipOrWarn(DaggerApplication.getIns(), response.getMessage());
//                ((IGetCodeContact.IGetCodeView) mView).getCodeFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                Toast.makeText(DaggerApplication.getIns(), throwable.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void toAuth(String cardNo, String checkCode, String idNo, String phone, String name) {
        SpecialAuthReq specialAuthReq = new SpecialAuthReq();
        specialAuthReq.setCardNo(cardNo);
        specialAuthReq.setCheckCode(checkCode);
        specialAuthReq.setIdNo(idNo);
        specialAuthReq.setMobile(phone);
        specialAuthReq.setName(name);
        mNetworkDateSource.verifySpecialAccount(specialAuthReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                ((ISpecialAuthContact.ISpecialAuthView) mView).toAuthSucc(response);
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                ((ISpecialAuthContact.ISpecialAuthView) mView).toAuthFail(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });

    }

}
